package cn.edu.zjicm.wordsnet_d.synchronize;

import java.io.IOException;
import java.util.Arrays;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryBuffer;

import cn.edu.zjicm.wordsnet_d.data.SynData;

public class ThriftSerialization {

	public static byte[] serialize(SynData obj) throws TException, IOException {
		long start = System.currentTimeMillis();
		TMemoryBuffer mb0 = new TMemoryBuffer(32); // 申请内存 TMemoryBuffer
		TProtocol prot0 = new TBinaryProtocol(mb0); // 在内存上构建TBinaryProtocol
													// TCompactProtocol
		obj.write(prot0); // 序列化,将obj序列化到prot0@mb0
		byte[] data = mb0.getArray();
		data = Arrays.copyOf(data, mb0.length());
		return data;
	}

	public static SynData deserialize(byte[] by) throws TException, IOException {
		long start = System.currentTimeMillis();
		TMemoryBuffer readMB = new TMemoryBuffer(32);
		TProtocol tp = new TBinaryProtocol(readMB);
		readMB.write(by);
		SynData synData = new SynData();
		synData.read(tp);
		return synData;
	}
}
